var wall_post_types = [];
var wall_icons = 'images/icons/wall_icons.gif';

function add_graffiti_post_type(to_id, label) {
  wall_post_types.push({
    url: base_domain + 'graffiti.php?act=draw&' + (to_id < 0 ? ('group_id=' + (-to_id)) : ('to_id=' + to_id)),
    icon: wall_icons,
    bgpos: [0, 0],
    label: label,
    onClick: function() { return drawGraffiti(to_id); }
  });
}
function add_video_post_type(to_id, label) {
  var url = base_domain + 'video.php?to_id=' + to_id;
  wall_post_types.push({
    url: url,
    icon: wall_icons,
    bgpos: [0, -22],
    label: label,
    onClick: function() { return chooseVideo(to_id, url); }
  });
}
function add_photo_post_type(to_id, label) {
  var url = base_domain + 'photos.php?to_id=' + to_id;
  wall_post_types.push({
    url: url,
    icon: wall_icons,
    bgpos: [0, -44],
    label: label,
    onClick: function() { return choosePhoto(to_id, url); }
  });
}
function add_audio_post_type(to_id, label) {
  var url = base_domain + 'audio.php?to_id=' + to_id;
  wall_post_types.push({
    url: url,
    icon: wall_icons,
    bgpos: [0, -66],
    label: label,
    onClick: function() { return chooseAudio(to_id, url); }
  });
}
function add_app_post_type(to_id, app_id, name, icon) {
  var url = base_domain + 'app' + app_id + '?to_id=' + to_id;
  wall_post_types.push({
    url: url,
    icon: icon,
    bgpos: [3, 3],
    label: name,
    onClick: function() { return chooseApp(app_id, to_id, url); }
  });
}

var add_wall_hide_timeout = false;
function show_add_wall_media() {
  clearTimeout(add_wall_hide_timeout);
  onDomReady(function() {
    var elem = ge('add_wall_menu');
    if (!isVisible(elem)) {
      var lnk = ge('add_wall_media_link');
      lnk.blur();
      var coords = getXY(lnk);
      elem.style.left = (coords[0] - 8 + (browser.msie6 ? 1 : 0)) + 'px';
      elem.style.top = (coords[1] - 4 + (browser.msie && !browser.msie8 ? 1 : 0)) + 'px';
      if (/mac/.test(_ua) && browser.mozilla) {
        ge('add_wall_rows').style.top = '22px';
      }
      browser.msie ? show(elem) : fadeIn(elem, 100);
    }
  });
}
function hide_add_wall_media(noTimeout) {
  var hideFunc = function() {
    browser.msie ? hide('add_wall_menu') : fadeOut(ge('add_wall_menu'), 100);
  }
  if (noTimeout == true) hideFunc();
  else add_wall_hide_timeout = setTimeout(hideFunc, 300);
}

onDomReady(function() {
  if (wall_post_types.length) {
    var wall_icons = base_domain + 'images/icons/wall_icons.gif';

    var plwi = new Image();
    plwi.src = wall_icons;

    addEvent(ge('add_wall_menu'), 'mouseover', show_add_wall_media);
    addEvent(ge('add_wall_menu'), 'mouseout', hide_add_wall_media);
    var rows = '', spec_style = (/mac/.test(_ua) && browser.mozilla) ? 'height: 19px; padding-top: 3px;' : '';
    for (var i in wall_post_types) {
      var row = wall_post_types[i];
      if (row.icon.substr(0, 7) != 'http://' && row.icon.substr(0, 1) != '/') {
        row.icon = base_domain + row.icon;
      }
      rows += '<a onfocus="this.blur()" id="wall_post_type' + i + '" style="background-image: url(' + row.icon + '); background-position: ' + row.bgpos[0] + 'px ' + row.bgpos[1] + 'px;' + spec_style + '" href="' + row.url + '">' + row.label.replace(/\s/g, '&nbsp;') + '</a>';
    }
    ge('add_wall_rows').innerHTML = '<table cellspacing="0" cellpadding="0"><tr><td class="add_wall_side"><div>&nbsp;</div></td><td><div class="rows">' + rows + '</div><div class="add_wall_bottom"></div><div class="add_wall_bottom2"></div></td><td class="add_wall_side"><div>&nbsp;</div></td></tr></table>';
    for (var i in wall_post_types) {
      addEvent(ge('wall_post_type' + i), 'click', (function(onClick) {
        return function() {
          hide_add_wall_media(true);
          return onClick();
        }
      })(wall_post_types[i].onClick));
    }
    addEvent(ge('add_wall_header'), 'click', function() { hide_add_wall_media(true); });
  }
});

var six_members_page = 0;
function getSixMembers(url, params) {
  if (isVisible(ge('progr'))) {
    return;
  }
  show('progr');
  params.page = (++six_members_page);
  Ajax.Post({url: url, query: params, onDone: function(obj, text) {
    hide('progr');
    ge('fBox').innerHTML = parseResponse(text);
  }, onFail: function() {
    hide('progr');
  }});
}

function getWallPage(oid, page) {
  if (isVisible(ge('progr2'))) {
    return;
  }
  show('progr2');
  Ajax.Post({url: 'wall.php', query: {act: 'get10', oid: oid, page: page}, onDone: function(obj, text) {
    hide('progr2');
    ge('fBox2').innerHTML = parseResponse(text);
  }, onFail: function() {
    hide('progr2');
  }});
}

function parseResponse(response) {
  response = response.replace(/^[\s\n]+/g, '');

  if (response.substr(0,10)=="<noscript>") {
    try {
      var arr = response.substr(10).split("</noscript>");
      eval(arr[0]);
      return arr[1];
    } catch(e) {
      return response;
    }
  } else {
    return response;
  }
}

function restorePost(cid, oid, from_wall) {
  from_wall = from_wall ? 1 : 0;
  var ajax = new Ajax();
  ajax.cid = cid;
  ajax.onDone = function(obj, text) {
    ge('wPostContainer' + obj.cid).innerHTML = '<div id="wPostContent' + obj.cid + '">' + ge('wPostContent' + obj.cid).innerHTML + '</div>';
    if (text != "good") {
      replacePostContent(obj.cid, text);
    }
  }
  ajax.onFail = postFailed;
  ajax.post('/wall.php', {'act': 'a_restore', 'oid': oid, 'cid': cid, 'from_wall': from_wall});
}

function deletePost(cid, oid) {
  var ajax = new Ajax();
  ajax.cid = cid;
  ajax.onDone = postDeleted;
  ajax.onFail = postFailed;
  ajax.post('/wall.php', {'act': 'a_delete', 'oid': oid, 'cid': cid});
}

function reportSpamPost(cid, oid) {
  var ajax = new Ajax();
  ajax.cid = cid;
  ajax.onDone = postDeleted;
  ajax.onFail = postFailed;
  ajax.post('/wall.php', {'act': 'a_report_spam', 'oid': oid, 'cid': cid});
}

function postDeleted(ajaxObj, responseText) {
  replacePostContent(ajaxObj.cid, responseText);
};

function postFailed(ajaxObj, responseText) {
  replacePostContent(ajaxObj.cid, '<div class="msg_wall" id="wResult' + ajaxObj.cid + '">Server error.</div>');
}

function replacePostContent(cid, text) {
  ge('wPostContent' + cid).style.display = 'none';
  ge('wPostContainer' + cid).innerHTML += text;
  var adding = '';
  if (ge('dArrow' + cid)) {
    adding += '<div style="position: relative; top: -6px;" class="dArrow">' + ge('dArrow' + cid).innerHTML + '</div>';
  }
  if (ge('upArrow' + cid)) {
    adding += '<div style="position: relative; top: -6px;" class="upArrow">' + ge('upArrow' + cid).innerHTML + '</div>';
  }
  if (adding.length && ge('wResult' + cid)) {
    ge('wResult' + cid).innerHTML = adding + ge('wResult' + cid).innerHTML;
  }
}

function banUser(act, mid, gid, hash, msgid){
  var a = new Ajax(function(res, text) {
    ge(msgid).innerHTML = text;
  }, function(res, text) {
    var prev = ge(msgid).innerHTML;
    ge(msgid).innerHTML = text;
    setTimeout(function() {
      ge(msgid).innerHTML = prev;
    }, 5000);
  });
  a.post('/ban.php', {act:act, mid:mid, gid:gid, hash:hash});
}

var deleting_all_id = 0;

function toggleDeleteAll(message_id) {
  toggle('wConfirm' + message_id);
  if (isVisible('wConfirm' + message_id)) {
    hide('wConfirm' + deleting_all_id);
    removeEvent(document, 'keydown');
    addEvent(document, 'keydown', function(e) { if (e.keyCode == 27) { toggleDeleteAll(message_id); }});
    deleting_all_id = message_id;
  } else {
    removeEvent(document, 'keydown');
    deleting_all_id = 0;
  }
}

var graffitiBox = null;
function showGraffiti(grid, owner, elem) {
  if (window.event && (window.event.which == 2 || window.event.button == 1)) {
    return true;
  }
  if (!graffitiBox) {
    graffitiBox = new MessageBox({width: 606, bodyStyle: 'padding: 10px', closeButton: true, fullPageLink: elem.href});
  }
  graffitiBox.removeButtons();
  graffitiBox.addButton({label: global_close, onClick: function() {
    graffitiBox.hide();
  }});
  graffitiBox.addControlsText(ge('graffiti_author' + grid).innerHTML);
  graffitiBox.setOptions({title: ge('graffiti_title' + owner).value, fullPageLink: elem.href});

  graffitiBox.content('<div style="width: 586px; overflow: hidden; height: 293px; background: #fff url(' + base_domain + 'images/progress7.gif) no-repeat 50% 50%" onclick="graffitiBox.hide()"><img src="' + ge('graffiti_src' + grid).value + '" /></div>').show();
  return false;
}

var wallPhotoBox = null;
var wallImages = {};
function showWallPhoto(phid, owner, elem) {
  if (window.event && (window.event.which == 2 || window.event.button == 1)) {
    return true;
  }
  if (!wallImages[phid]) {
    wallImages[phid] = new Image();
    wallImages[phid].src = ge('wallphoto_src' + phid).value;
  }
  setTimeout(function() {
    var width = wallImages[phid].width;
    if (!wallPhotoBox) {
      wallPhotoBox = new MessageBox({bodyStyle: 'padding: 10px', closeButton: true, fullPageLink: elem.href});
    }
    wallPhotoBox.removeButtons();
    wallPhotoBox.addButton({label: global_close, onClick: function() {
      wallPhotoBox.hide();
    }});
    wallPhotoBox.addControlsText(ge('wallphoto_author' + phid).innerHTML);
    wallPhotoBox.setOptions({width: (intval(width) > 410) ? (width + 20) : 430, title: ge('wallphoto_title' + owner).value, fullPageLink: elem.href});
    if (wallImages[phid].height) {
      wallPhotoBox.content('<div style="height: ' + wallImages[phid].height + 'px; text-align: center;" onclick="wallPhotoBox.hide()"><img style="background: #fff url(' + base_domain + 'images/progress7.gif) no-repeat 50% 50%" src="' + ge('wallphoto_src' + phid).value + '" /></div>').show();
    } else {
      wallPhotoBox.content('<div class="box_loader"></div>').show();
      var updateBox = function() {
        if (wallImages[phid].height) {
          var width = (wallImages[phid].width > 410) ? (wallImages[phid].width + 20) : 430;
          wallPhotoBox.setOptions({width: width});
          wallPhotoBox.content('<div style="height: ' + wallImages[phid].height + 'px; text-align: center;" onclick="wallPhotoBox.hide()"><img style="background: #fff url(' + base_domain + 'images/progress7.gif) no-repeat 50% 50%" src="' + ge('wallphoto_src' + phid).value + '" /></div>');
        } else {
          setTimeout(updateBox, 50);
        }
      }
      setTimeout(updateBox, 50);
    }
  }, 0);
  return false;
}

var wallVideoBox = null;
function showWallVideo(vid, owner, elem) {
  if (window.event && (window.event.which == 2 || window.event.button == 1)) {
    return true;
  }
  if (!wallVideoBox) {
    wallVideoBox = new MessageBox({width: 480, bodyStyle: 'padding: 10px', closeButton: true, fullPageLink: elem.href, onHideAttempt: function() {
      wallVideoBox.content('');
      return true;
    }});
  }
  wallVideoBox.removeButtons();
  wallVideoBox.addButton({label: global_close, onClick: function() {
    wallVideoBox.hide();
  }});
  wallVideoBox.addControlsText(ge('wallvideo_author' + vid).innerHTML);
  wallVideoBox.setOptions({title: ge('wallvideo_title' + owner).value, fullPageLink: elem.href});

  wallVideoBox.content('<div style="width: 460px; height: 345px;" id="videoBoxContent"><div id="videoBoxNoFlash" style="text-align: center; position: absolute; display: none; border: 1px solid #ccc; padding: 10px; background: #fff"></div><img style="width: 460px; height: 345px;" src="' + ge('wallvideo_thumb' + vid).value + '" /></div><div style="padding-top: 10px">' + ge('wallvideo_desc' + vid).innerHTML + '</div>').show();

  var so = new SWFObject(base_domain + 'swf/VideoPlayer.swf?8', 'videoBoxPlayer', '460', '345', '9');
  so.addParam('allowfullscreen', 'true');
  so.addParam('allowscriptaccess', 'always');
  so.setAttribute('preventHide', '1');

  var flashVars = eval('(' + ge('wallvideo_vars' + vid).value + ')');
  flashVars.overstretch = 'false';
  flashVars.vid = '';
  for (var i in flashVars) {
    so.addVariable(i, flashVars[i]);
  }

  if (so.installedVer.major >= 9) {
    so.write('videoBoxContent');
    setTimeout(function() { ge('videoBoxPlayer').style.visibility = 'visible'; }, 1);
  } else {
    ge('videoBoxNoFlash').innerHTML = ge('wallvideo_needplayer').value;
    show('videoBoxNoFlash');
    var size = getSize(ge('videoBoxNoFlash'));
    ge('videoBoxNoFlash').style.marginLeft = intval((460 - size[0]) / 2) + 'px';
    ge('videoBoxNoFlash').style.marginTop  = intval((345 - size[1]) / 2) + 'px';
  }
  return false;
}

var drawBox = null, drawWarningBox = null, drawNeedConfirm = false;
window.posting_on_wall = false;
function doCancelDrawing() {
  if (!drawBox) return;
  drawBox.setOptions({onHideAttempt: false});
  drawBox.hide();
}
function drawGraffiti(to_id) {
  if (window.event && (window.event.which == 2 || window.event.button == 1)) {
    return true;
  }
  if (!drawBox) {
    drawBox = new MessageBox({title: ge('draw_graffiti_title').value, width: 610, bodyStyle: 'padding: 0px', type: 'POPUP', hideOnClick: false, progress: 'graffiti_draw_progress', onLoad: function() {
      if (ge('graffitiDraw')) {
        drawBox.setOptions({onHideAttempt: function() {
          var result = ge('graffitiDraw').cancelDrawing();
          if (result) {
            ge('graffitiDrawing').innerHTML = '';
          }
          return result;
        }});
        ge('graffitiDrawing').style.height = '483px';
      } else {
        drawBox.setOptions({width: 400, bodyStyle: 'padding: 16px 14px', type: 'MESSAGE'});
        drawBox.addButton({label: global_close, onClick: function() { doCancelDrawing(); }});
      }
    }});
  } else {
    drawBox.setOptions({width: 610, bodyStyle: 'padding: 0px', type: 'POPUP'});
  }
  drawBox.removeButtons();
  drawBox.loadContent('graffiti.php', {act: 'a_draw_box', to_id: to_id}, true).show();
  return false;
}
function graffitiUploaded() {
  if (posting_on_wall) {
    return;
  }
  posting_on_wall = drawNeedConfirm = true;
  page = page + 1;
  var params = {act: 'get10', wall_hash: window.wall_post_hash, wpage: 100000, graffiti: 1, fpage: page, mid: ge('to_id').value, n: 3};
  if (isVisible(ge('r'))) {
    params.seed = ge('reply_field').value;
  }
  var options = {onSuccess: function(obj, text) {
    posting_on_wall = drawNeedConfirm = false;
    hide('msg', 'msg_graffiti', 'msg_photo', 'msg_video', 'msg_audio', 'r', 'graffiti_draw_progress');
    show('msg_graffiti');
    ge('fBox2').innerHTML = parseResponse(text);
    doCancelDrawing();
  }, onFail: function() {
    posting_on_wall = false;
    hide('graffiti_draw_progress');
  }, onCaptchaShow: function() {
    drawBox.setOptions({onHide: null});
    doCancelDrawing();
  }, onCaptchaHide: function(done) {
    if (done) return;
    posting_on_wall = false;
    hide('graffiti_draw_progress');
    drawBox.setOptions({width: 606, bodyStyle: 'padding: 10px', type: 'MESSAGE', onHide: function() {
      if (!drawWarningBox) {
        drawWarningBox = new MessageBox({title: global_warning});
        drawWarningBox.content(window.lang_graffiti_cancel_confirm);
        drawWarningBox.addButton({label: global_cancel, style: 'button_no', onClick: function() { drawWarningBox.hide(); }});
        drawWarningBox.addButton({label: global_continue, onClick: function() { drawNeedConfirm = false; drawWarningBox.setOptions({onHide: null}); drawWarningBox.hide(); }});
      }
      drawWarningBox.setOptions({onHide: function() { drawBox.show(); }});
      drawWarningBox.show();
    }});
    drawBox.removeButtons();
    drawBox.addButton({label: global_cancel, style: 'button_no', onClick: function() { drawBox.hide(); }});
    drawBox.addButton({label: window.lang_graffiti_send, onClick: function() {
      show('graffiti_draw_progress');
      drawBox.setOptions({onHide: null});
      graffitiUploaded();
    }});
    drawBox.content('<div id="uploadedGraffiti" style="width: 586px; height: 293px; background: #fff url(' + base_domain + 'images/progress7.gif) no-repeat 50% 50%; overflow: hidden"></div>').show();
    Ajax.Post({url: 'graffiti.php', query: {act: 'a_get_last'}, onDone: function(obj, text) {
      if (text && ge('uploadedGraffiti')) {
        ge('uploadedGraffiti').innerHTML = '<img style="width: 586px; height: 293px" src="' + text + '" />';
      } else if (drawBox) {
        drawBox.content(global_unknown_error);
        drawBox.removeButtons();
        drawBox.addButton({label: global_cancel, style: 'button_no', onClick: function() { drawBox.hide(); }});
      }
    }});
  }};
  Ajax.postWithCaptcha('wall.php', params, options);
}
var flashNeedConfirm = false;
function confirmWhenExit() {
  flashNeedConfirm = true;
}
window.onbeforeunload = function() {
  flashNeedConfirm = false;
  if (ge('graffitiDraw') && drawBox.isVisible()) ge('graffitiDraw').confirmWhenExit();
  if (drawNeedConfirm || flashNeedConfirm) {
    return window.lang_graffiti_are_you_sure;
  }
}

var showAppBox = null;
function showApp(app_id, to_id, from_id, loc, href) {
  if (window.event && (window.event.which == 2 || window.event.button == 1)) {
    return true;
  }
  if (!showAppBox) {
    attachScript('appsFlash', base_domain+'js/apps_flash.js');
    showAppBox = new MessageBox({title: ge('show_app_title').value, width: 609, closeButton: true, fullPageLink: href, bodyStyle: 'height: 412px; padding: 0px;', onHideAttempt: function() {
      showAppBox.content('');
      return true;
    }});
  }
  showAppBox.setOptions({fullPageLink: href});
  showAppBox.removeButtons().addButton({label: global_close, onClick: showAppBox.hide}).addControlsText('<a href="'+href+'">'+ge('go_to_app_label').value+'</a>');
  showAppBox.loadContent('apps.php', {act: 'a_show_app_box', to_id: to_id, from_id: from_id, loc: loc, aid: app_id}, true, 'height: 412px').show();
  return false;
}

var chooseAppBox = null;
function chooseApp(app_id, to_id, href) {
  if (window.event && (window.event.which == 2 || window.event.button == 1)) {
    return true;
  }
  href = href.href ? href.href : href;
  if (!chooseAppBox) {
    attachScript('appsFlash', base_domain+'js/apps_flash.js');
    chooseAppBox = new MessageBox({title: ge('choose_app_title').value, width: 609, progress: 'choose_app_progress', closeButton: true, fullPageLink: href, bodyStyle: 'height: 412px; padding: 0px;', onHideAttempt: function() {
      chooseAppBox.content('');
      return true;
    }});
    chooseAppBox.addButton({label: global_close, onClick: chooseAppBox.hide});
  }
  hide('choose_app_progress');
  chooseAppBox.loadContent('apps.php', {act: 'a_choose_app_box', to_id: to_id, aid: app_id}, true, 'height: 412px').show();
  return false;
}
function doSendAppPost(dataHash) {
  if (posting_on_wall || !chooseAppBox || !chooseAppBox.isVisible()) {
    return;
  }
  posting_on_wall = true;
  page = page + 1;
  show('choose_app_progress');
  var params = {act: 'get10', wall_hash: window.app_wall_hash, wpage: 100000, data_hash: dataHash, fpage: page, mid: ge('to_id').value, n: 3};
  if (isVisible(ge('r'))) {
    params.seed = ge('reply_field').value;
  }
  var options = {onSuccess: function(obj, text) {
    posting_on_wall = false;
    hide('msg', 'msg_graffiti', 'msg_photo', 'msg_video', 'msg_audio', 'r', 'choose_app_progress');
    show('msg_photo');
    if (text.substr(0, 8) == 'redirect') {
      location.replace(text.substr(9));
      return;
    }
    ge('fBox2').innerHTML = parseResponse(text);
    chooseAppBox.hide();
  }, onFail: function() {
    posting_on_wall = false;
  }, onCaptchaShow: function() {
    posting_on_wall = false;
  }, onCaptchaHide: function(done) {
    chooseAppBox.show();
    chooseAppBox.fixIE6();
    ge('choose_app_progress').style.display = 'none';
  }};
  Ajax.postWithCaptcha('wall.php', params, options);
}

var chooseAudioBox = null;
function chooseAudio(to_id, href) {
  if (window.event && (window.event.which == 2 || window.event.button == 1)) {
    return true;
  }
  href = href.href ? href.href : href;
  if (!chooseAudioBox) {
    chooseAudioBox = new MessageBox({title: ge('choose_audio_title').value, width: 610, progress: 'choose_audio_progress', closeButton: true, fullPageLink: href, bodyStyle: 'height: 412px; padding: 0px;', onHide: function() {
      if (AudioObject.curAudio && AudioObject.curAudio.substr(0, 6) == 'Choose') {
        AudioObject.hidePlayer(AudioObject.curAudio, false, true);
        AudioObject.curAudio = null;
      }
    }});
    chooseAudioBox.addButton({label: global_close, onClick: chooseAudioBox.hide});
  }
  ge('choose_audio_progress').style.display = 'none';
  chooseAudioBox.loadContent('audio.php', {act: 'a_choose_audio_box', to_id: to_id}, true, 'height: 412px').show();
  return false;
}
function doSendAudio(id, owner) {
  if (posting_on_wall) {
    return;
  }
  posting_on_wall = true;
  audio_owner = owner ? owner : 0;
  page = page + 1;
  show('choose_audio_progress');
  var params = {act: 'get10', wall_hash: window.wall_post_hash, wpage: 100000, audio_owner: audio_owner, audio: id, fpage: page, mid: ge('to_id').value, n: 3};
  if (isVisible(ge('r'))) {
    params.seed = ge('reply_field').value;
  }
  var options = {onSuccess: function(obj, text) {
    posting_on_wall = false;
    hide('msg', 'msg_graffiti', 'msg_photo', 'msg_video', 'msg_audio', 'r', 'choose_audio_progress');
    show('msg_audio');
    ge('fBox2').innerHTML = parseResponse(text);
    chooseAudioBox.hide();
  }, onFail: function() {
    posting_on_wall = false;
  }, onCaptchaShow: function() {
    posting_on_wall = false;
  }, onCaptchaHide: function(done) {
    chooseAudioBox.show();
    chooseAudioBox.fixIE6();
    ge('choose_audio_progress').style.display = 'none';
  }};
  Ajax.postWithCaptcha('wall.php', params, options);
}

var chooseVideoBox = null;
function chooseVideo(to_id, href) {
  if (window.event && (window.event.which == 2 || window.event.button == 1)) {
    return true;
  }
  href = href.href ? href.href : href;
  if (!chooseVideoBox) {
    chooseVideoBox = new MessageBox({title: ge('choose_video_title').value, width: 610, progress: 'choose_video_progress', closeButton: true, fullPageLink: href, bodyStyle: 'height: 412px; padding: 0px;'});
    chooseVideoBox.addButton({label: global_close, onClick: chooseVideoBox.hide});
  }

  if (!window.scrollbarWidth) {
    var test = document.createElement("div");
    with (test.style) {
      overflowY = "scroll";
      position = "absolute";
      height = "100px";
      width = "100px";
    }
    test.innerHTML = '<div style="height:200px;">1<br/>1<br/>1<br/>1<br/></div>';
    var body = document.getElementsByTagName('body')[0];
    body.appendChild(test);
    window.scrollbarWidth = test.offsetWidth - test.getElementsByTagName('div')[0].offsetWidth - 1;
    body.removeChild(test);
    delete test;
  }

  ge('choose_video_progress').style.display = 'none';
  chooseVideoBox.loadContent('video.php', {act: 'a_choose_video_box', to_id: to_id, scrollbar_width: window.scrollbarWidth}, true, 'height: 412px').show();
  return false;
}
function doSendVideo(id, owner) {
  if (posting_on_wall) {
    return;
  }
  posting_on_wall = true;
  video_owner = owner ? owner : 0;
  page = page + 1;
  show('choose_video_progress');
  var params = {act: 'get10', wall_hash: window.wall_post_hash, wpage: 100000, video_owner: video_owner, video: id, fpage: page, mid: ge('to_id').value, n: 3};
  if (isVisible(ge('r'))) {
    params.seed = ge('reply_field').value;
  }
  var options = {onSuccess: function(obj, text) {
    posting_on_wall = false;
    hide('msg', 'msg_graffiti', 'msg_photo', 'msg_video', 'msg_audio', 'r', 'choose_video_progress');
    show('msg_video');
    ge('fBox2').innerHTML = parseResponse(text);
    chooseVideoBox.hide();
  }, onFail: function() {
    posting_on_wall = false;
  }, onCaptchaShow: function() {
    posting_on_wall = false;
  }, onCaptchaHide: function(done) {
    chooseVideoBox.show();
    chooseVideoBox.fixIE6();
    ge('choose_video_progress').style.display = 'none';
  }};
  Ajax.postWithCaptcha('wall.php', params, options);
  return false;
}

var choosePhotoBox = null, newUploadedBox = null;
function choosePhoto(to_id, href) {
  if (window.event && (window.event.which == 2 || window.event.button == 1)) {
    return true;
  }
  href = href.href ? href.href : href;
  if (!choosePhotoBox) {
    choosePhotoBox = new MessageBox({title: ge('choose_photo_title').value, width: 610, progress: 'choose_photo_progress', closeButton: true, fullPageLink: href, bodyStyle: 'height: 412px; padding: 0px;'});
    choosePhotoBox.addButton({label: global_close, onClick: choosePhotoBox.hide});
  }

  if (!window.scrollbarWidth) {
    var test = document.createElement("div");
    with (test.style) {
      overflowY = "scroll";
      position = "absolute";
      height = "100px";
      width = "100px";
    }
    test.innerHTML = '<div style="height:200px;">1<br/>1<br/>1<br/>1<br/></div>';
    var body = document.getElementsByTagName('body')[0];
    body.appendChild(test);
    window.scrollbarWidth = test.offsetWidth - test.getElementsByTagName('div')[0].offsetWidth - 1;
    body.removeChild(test);
    delete test;
  }

  ge('choose_photo_progress').style.display = 'none';
  choosePhotoBox.loadContent('photos.php', {act: 'a_choose_photo_box', to_id: to_id, scrollbar_width: window.scrollbarWidth}, true, 'height: 412px').show();
  return false;
}
function doSendPhoto(id) {
  if (posting_on_wall) {
    return;
  }
  posting_on_wall = true;
  page = page + 1;
  show('choose_photo_progress');
  var params = {act: 'get10', wall_hash: window.wall_post_hash, wpage: 100000, photo: id, fpage: page, mid: ge('to_id').value, n: 3};
  if (isVisible(ge('r'))) {
    params.seed = ge('reply_field').value;
  }
  var options = {onSuccess: function(obj, text) {
    posting_on_wall = false;
    hide('msg', 'msg_graffiti', 'msg_photo', 'msg_video', 'msg_audio', 'r', 'choose_photo_progress');
    show('msg_photo');
    ge('fBox2').innerHTML = parseResponse(text);
    choosePhotoBox.hide();
  }, onFail: function() {
    posting_on_wall = false;
  }, onCaptchaShow: function() {
    posting_on_wall = false;
  }, onCaptchaHide: function(done) {
    choosePhotoBox.show();
    choosePhotoBox.fixIE6();
    ge('choose_photo_progress').style.display = 'none';
  }};
  Ajax.postWithCaptcha('wall.php', params, options);
  return false;
}
function doSendNewUploaded(server, photo) {
  if (posting_on_wall) {
    return;
  }
  posting_on_wall = true;
  page = page + 1;
  show('new_uploaded_progress');
  var params = {act: 'get10', wall_hash: window.wall_post_hash, wpage: 100000, posted_server: server, posted_photo: photo, fpage: page, mid: ge('to_id').value, n: 3};
  if (isVisible(ge('r'))) {
    params.seed = ge('reply_field').value;
  }
  var options = {onSuccess: function(obj, text) {
    posting_on_wall = false;
    hide('msg', 'msg_graffiti', 'msg_photo', 'msg_video', 'msg_audio', 'r', 'new_uploaded_progress');
    show('msg_photo');
    ge('fBox2').innerHTML = parseResponse(text);
    newUploadedBox.setOptions({onHide: false});
    newUploadedBox.hide();
  }, onFail: function() {
    posting_on_wall = false;
  }, onCaptchaShow: function() {
    posting_on_wall = false;
  }, onCaptchaHide: function(done) {
    newUploadedBox.show();
    newUploadedBox.setOptions({onHide: choosePhotoBox.show});
    ge('new_uploaded_progress').style.display = 'none';
  }};
  Ajax.postWithCaptcha('wall.php', params, options);
  return false;
}

function addMeAudio(wid, oid, aid, hash) {
  if (posting_on_wall) {
    return;
  }
  posting_on_wall = true;
  show('adding' + wid + 'progress');
  var failed = function() {
    hide('adding' + wid + 'progress');
    posting_on_wall = false;
  }
  Ajax.postWithCaptcha('audio.php', {act: 'a_add', aid: aid, oid: oid, hash: hash, from_profile: ge('my_audios') ? 1 : ''}, {onSuccess: function(obj, text) {
    hide('adding' + wid + 'progress');
    posting_on_wall = false;
    if (ge('my_audios')) {
      var response = eval('(' + text + ')');
      ge('my_audios').innerHTML = response.html;
      ge('audios_how_much').innerHTML = '(' + response.how_much + ')';
      if (response.script) eval(response.script);
    }
    show('adding' + wid + 'done');
    hide('adding' + wid + 'link');
    var prev_node = ge('adding' + wid + 'link').previousSibling, next_node = ge('adding' + wid + 'link').nextSibling;
    if (prev_node && prev_node.tagName && prev_node.tagName.toLowerCase() == 'span') {
      hide(prev_node);
    } else if (next_node && next_node.tagName && next_node.tagName.toLowerCase() == 'span') {
      hide(next_node);
    }
    setTimeout(function() { fadeOut(ge('adding' + wid + 'done'), 500); }, 1000);
  }, onFail: failed, onCaptchaShow: failed});
}
function addMeVideo(wid, oid, vid, hash) {
  if (posting_on_wall) {
    return;
  }
  posting_on_wall = true;
  show('adding' + wid + 'progress');
  var failed = function() {
    hide('adding' + wid + 'progress');
    posting_on_wall = false;
  }
  Ajax.postWithCaptcha('video.php', {act: 'a_add', vid: vid, oid: oid, hash: hash, from_profile: ge('my_videos') ? 1 : ''}, {onSuccess: function(obj, text) {
    hide('adding' + wid + 'progress');
    posting_on_wall = false;
    if (ge('my_videos')) {
      var response = eval('(' + text + ')');
      ge('my_videos').innerHTML = response.html;
      ge('videos_how_much').innerHTML = '(' + response.how_much + ')';
    }
    show('adding' + wid + 'done');
    hide('adding' + wid + 'link');
    var prev_node = ge('adding' + wid + 'link').previousSibling, next_node = ge('adding' + wid + 'link').nextSibling;
    if (prev_node && prev_node.tagName && prev_node.tagName.toLowerCase() == 'span') {
      hide(prev_node);
    } else if (next_node && next_node.tagName && next_node.tagName.toLowerCase() == 'span') {
      hide(next_node);
    }
    setTimeout(function() { fadeOut(ge('adding' + wid + 'done'), 500); }, 1000);
  }, onFail: failed, onCaptchaShow: failed});
}

var lastTextLength = 0;
function checkTextLength(max_len, val, warn) {
  if (lastTextLength == val.length) {
    return;
  }
  lastTextLength = val.length;
  var l = 0;
  val = val.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(new RegExp('>', 'g'), '&gt;').replace(/"/g, '&quot;').replace(/\n/g, '<br>').replace(/\r/g, '').replace(/!/g, '&#33;').replace(/'/g, '&#39;');
  for (var i = 0; i < val.length; ++i) {
    var c = val.charCodeAt(i);
    if ((c > 0x80 && c < 0xC0) || c > 0x500) {
      l += (c + '').length + 3;
    } else {
      ++l;
    }
  }
  if (l > max_len) {
    var plus = l - max_len;
    warn.style.display = "";
    warn.innerHTML = langNumeric(plus, text_exceeds_symbol_limit);
  } else if (l > max_len - 100) {
    var rem = max_len - l;
    warn.style.display = "";
    warn.innerHTML = langNumeric(rem, text_N_symbols_remain);
  } else {
    warn.style.display = "none";
    warn.innerHTML = '';
  }
}